<?

/*	MOD SUPERCLASS
* This class contains all default functionalities for
* the module's public output.
* this can currently be extended with 1 medio-class:
*	modTEXT				string / search & regexp additions
*					*/

class MOD {
	
	function __construct( $vars ) {
		
		$this->_addvars( $vars );
		$this->_ref();
		
		if( $this->style ) $this->set_attribute( "style", $this->style );
	}
	
	function _addvars( $vars ){ 
		
		foreach($vars as $k=>$v) $this->$k = $v;
	}
	
	function _ref() { 
		$this->ref = json_decode( engine_get("style:default") );
		if($this->tleref) foreach($this->tleref as $k=>$v) $this->ref->title->{$k} = $v;
	}
	
	function output() {
		return $this->parse();
		
	}
	
	function ajax() {
	
		if( $this->action )
			$result = $this->{ "ajax_" . $this->action }();
			
		return $result;
	
	}
	
	function render_js() {
	
		return ( $this->js )? "<script type='text/javascript'>\n" . $this->js . "\n</script>" : "";
	}
	
	function print_vars() {
		return json_encode( $this->vars );
	}
	
	function render_attributes( $attr = false ) {
		
		if( $attr )
			foreach( $attr as $key => $value ) $attr[ $key ] = "$key = \"$value\"";
		
		return ( $attr )? implode( " ", $attr ): "";
	}
	
#	SET FUNCTIONALITIES: Edit mod content.

	function set_attribute( $key, $value ) {
	
		$this->attr[ $key ] = is_string( $value )?
			$value : json_encode( $value );

	}
	
	function addclass( $string ) {
	
		$this->classes[] = $string;
	}

	function set_title( $string, $position = false ) {
	
		$this->name = $string;
		if( $position )
			$this->ref->title->position = $position;
	}
	
	function unshift_content() {
		global $Engine;
		
		$content = $Engine->text->get( $this->textid, "long" );
		unset( $this->textid );
		return $content;
	}
	

#	PARSE FUNCTIONALITIES: HTML builders.

	function parse( $content = "" ) {
		global $Engine;
				
		$content = $this->sanitize( $content );


		if( $this->nameid ) $this->name = $Engine->text->get( $this->nameid ) . $this->name;
		if( $this->textid && $this->text = $Engine->text->get( $this->textid, "long" )) $content = $this->textreverse? $this->text . $content : $content . $this->text;
		if( $this->push_content ) $content .= $this->push_content;
		

		if( $this->name ) 
			$content = ( $this->ref->title->position == "first" )?
				$this->parse_title() . $this->parse_starttag() . $content : $this->parse_starttag() . $this->parse_title() . $content;
		else $content = $this->parse_starttag() . $content;

		return $this->unshift . $content . $this->parse_endtag();
	}
	
	function parse_starttag() {
		
		if( $this->class ) $this->addclass( $this->class );
		
		$this->attr[ "class" ] = "mod ". $this->modclass . (( $this->classes )? " ". implode( " ", $this->classes ): "" );
		
		return "<div ". $this->render_attributes( $this->attr ) .">";
	}
	
	function parse_endtag() {
		return ( $this->ref->hasbottom )? "<div class='bottom'></div></div>": "</div>";
	}
	
	function parse_title() {
		if( $this->h ) $this->ref->title->h = $this->h;
		
		if( $this->ref->title->graphic && !$this->tleref->plain ) {
			$img = new MEDIA( $this->ref->title );
			return $img->_txt( $this->name );
		} else return "\n<".$this->ref->title->h." class='title'>{$this->name}</".$this->ref->title->h.">";
	}
	
	private function sanitize( $cont ) {
		global $Engine;
	
		return is_string( $cont )?
			$this->parse_pregmatch( $cont ) : ( is_array( $cont )? "<ul>" . implode( "\n", $cont ) . "</ul>" : ( is_object( $cont )? $this->parse_object( $cont ) : ( is_int( $cont )? $Engine->text->get( $cont, "long" ) : null )));
							
	}
	
	private function parse_object( ) {
		
		return "please finish: if object, content is divided in parts";
	}
	
	function parse_pregmatch($text){
		global $DB;
		$text = stripslashes($text);
		if(preg_match_all('/(<%)([^%]*)(%>)/',$text,$raw, PREG_SET_ORDER)){
			$replace = $replace_by = array();
			foreach($raw as $v) if(!in_array($v[0],$replace)){
				$replace[]=$v[0];
				//$v[2]=json_decode((strlen($v[2])>3)? $v[2]: $DB->short("json","moddump","ID=".$v[2]),true);
				$replace_by[] = " -%- ";// mod_get("single",key($v[2]),current($v[2]));
			}
			$text = str_replace($replace, $replace_by, $text);
		}
		return $text;
	}

}

?>